package com.diandi.code.service;

import com.diandi.code.util.FreemarkerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;

/**
 * Created by 袁恩光 on 2018/5/8.
 */
@Service
public class DbAutoService {

    private static final Logger logger = LoggerFactory.getLogger(DbAutoService.class);

    public ResponseEntity<byte[]> getResponseEntity(String modelName) throws Exception {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", "Db" + modelName + "Config.java");
        return new ResponseEntity<>(getDbTemplate(modelName).toByteArray(), headers, HttpStatus.CREATED);
    }

    private ByteArrayOutputStream getDbTemplate(String modelName) {
        logger.info("生成Db模板");
        ByteArrayOutputStream db = FreemarkerUtil.createStream("DemoDbConfig.java", modelName);
        return db;
    }

}
